package com.shd.edanpei.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;

import com.shd.edanpei.R;
import com.shd.edanpei.base.dialog.Loader;
import com.shd.edanpei.image.LoadImageCtrl;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseFragment<P extends BaseIPresenter> extends Fragment implements BaseIView {

    private Unbinder mUnbinder;
    protected P mPresenter;
    protected View mView;
    /**
     * 加载图片控制类
     */
    protected LoadImageCtrl loadImageCtrl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = null;
        if (setLayout() instanceof Integer) {
            rootView = inflater.inflate((Integer) setLayout(), container, false);
        } else if (setLayout() instanceof View) {
            rootView = (View) setLayout();
        }
        if (rootView != null) {
            mPresenter = bindPresenter();
            mUnbinder = ButterKnife.bind(this, rootView);
            initView(rootView, savedInstanceState);
            initListener();
        }
        mView = rootView;
        return rootView;
    }

    /**
     * 设置布局ID
     *
     * @return
     */
    protected abstract Object setLayout();

    /**
     * 绑定Presenter
     *
     * @return
     */
    protected abstract P bindPresenter();

    /**
     * 初始化
     *
     * @param savedInstanceState
     */
    protected abstract void initView(@Nullable View view, @Nullable Bundle savedInstanceState);

    /**
     * 初始化监听
     */
    protected abstract void initListener();

    @Override
    public void showLoading(@Nullable String str) {
        Loader.show(getContext(), str);
    }

    @Override
    public void stopLoading() {
        Loader.stop();
    }

    @Override
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    @Override
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getContext(), clz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void viewAnim(View view) {
        Animation shake = AnimationUtils.loadAnimation(getContext(), R.anim.shake);
        view.startAnimation(shake);
    }

    @Override
    public boolean etData(EditText... views) {
        for (int i = 0; i < views.length; i++) {
            if (TextUtils.isEmpty(views[i].getText().toString())) {
                viewAnim(views[i]);
                return false;
            }
        }
        return true;
    }

    @Override
    public FragmentActivity getContext() {
        return getActivity();
    }


    @Override
    public void onStart() {
        super.onStart();
        if (loadImageCtrl != null) {
            loadImageCtrl.onStart();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (loadImageCtrl != null) {
            loadImageCtrl.onStop();
        }
    }

    @Override
    public void onDestroy() {
        if (loadImageCtrl != null) {
            loadImageCtrl.onDestroy();
        }
        super.onDestroy();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

}
